<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .container {
            width: 300px;
            height: 200px;
            background: skyblue;
            position: relative;
            left: 20px;
            top: 30px;
        }

        h1 {
            width: 100%;
            /* height: 20px; */
            background-color: antiquewhite;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>移动</h1>
        <div contenteditable>
            可编辑内容
        </div>
    </div>
    <script>
        let target = document.querySelector("h1")
        let note = document.querySelector(".container")
        // target.addEventListener("mousedown", (e) => {
        //     console.log("鼠标按下")
        //     window.addEventListener("mousemove", (e) => {
        //         console.log("鼠标移动")
        //     })


        // })
        // window.addEventListener("mouseup", (e) => {
        //     console.log("鼠标抬起")
        //     window.removeEventListener("mousemove", (e) => {
        //         console.log("取消移动监听")
        //     })
        // })
        let ClientW = document.documentElement.clientWidth;
        let offSetW = note.offsetWidth;
        let ClientH = document.documentElement.clientHeight;
        let offSetH = note.offsetHeight;
        let maxTop = ClientH - offSetH;
        let maxLeft = ClientW - offSetW;
        target.onmousedown = function (e) {
            // console.info("e", e)
            // console.info("鼠标按下坐标X位置", e.clientX)
            // console.info("鼠标按下坐标Y位置", e.clientY);
            let x = e.clientX;
            let y = e.clientY
            // console.info("当前位置元素坐标X位置", e.x);
            // console.info("当前位置元素坐标Y位置", e.y);
            let react = note.getBoundingClientRect()
            // console.log(react)
            // console.info("当前位置元素坐标X位置", react.left);
            let ex = react.left;
            let ey = react.top
            // console.info("当前位置元素坐标Y位置", react.top);
            window.onmousemove = function (e) {
                console.log("当前位置", e.clientX)
                console.log("当前位置", e.clientX)
                console.log("移动位置", e.clientX - x)
                console.log("移动位置", e.clientX - y)
                console.log("坐标X位置", ex + e.clientX - x)
                console.log("坐标Y位置", ey + e.clientY - y)
                let left = ex + e.clientX - x;
                let top = ey + e.clientY - y;
                if (left < 0) {
                    left = 0
                } else if (left > maxLeft) {
                    left = maxLeft
                }
                if (top < 0) {
                    top = 0
                } else if (top > maxTop) {
                    top = maxTop
                }
                note.style.left = left + "px"
                note.style.top = top + "px"
            }
            window.onmouseup = function (e) {
                console.log("鼠标抬起")

                window.onmousemove = null;
                window.onmouseup = null
            }
        }

    </script>
</body>

</html>